<template>
  <div class="app-container">
    <!-- 查询条件区域-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
      <el-form-item label="参数名称" prop="param_name">
        <el-input
            v-model="queryParams.param_name"
            placeholder="请输入参数名称"
            clearable
            @keyup.enter.native="handleQuery"
            size="small"
        />
      </el-form-item>
      <el-form-item label="参数简称" prop="param_en">
        <el-input
            v-model="queryParams.param_en"
            placeholder="请输入参数简称"
            clearable
            @keyup.enter.native="handleQuery"
            size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!--增删改查按钮区域-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['nssc:alarm:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['nssc:alarm:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['nssc:alarm:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 数据列表区域-->
    <el-table v-loading="loading" :data="listAlarm" @selection-change="handleSelectionChange" :cell-style="{borderColor:'rgb(1 5 43)'}" :header-cell-style="{borderColor: 'rgb(1 5 43)'}">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="参数简称" align="center" prop="param_en"/>
      <el-table-column label="参数名称" align="center" prop="param_name"/>
      <el-table-column label="告警上限" align="center" prop="alarmUpper" />
      <el-table-column label="告警下限" align="center" prop="alarmLower"  />
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['nssc:alarm:edit']"
          >修改</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['nssc:alarm:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--列表分页区域-->
    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" style="background:rgb(1 5 43);"
    />
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px"  @close="handleCancle" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="参数类型" prop="param_id">
              <el-select v-model="form.param_id" placeholder="请选参数类型" clearable @change="getOptioncslx">
                <el-option
                    v-for="item in alarmParamList"
                    :key="item.id"
                    :label="item.param_name"
                    :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="告警上限" prop="alarmUpper">
              <el-input v-model="form.alarmUpper" placeholder="请输入告警上限值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="告警下限" prop="alarmLower">
              <el-input v-model="form.alarmLower" placeholder="请输入告警下限值" />
            </el-form-item>
          </el-col>
          <el-col :span="12" width="200px">
            <el-form-item label="上限描述" prop="alarm_upper_info">
              <el-input v-model="form.alarm_upper_info"  type="textarea" placeholder="请输入告警上限描述(0-255)" style="width: 200px;"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" width="200px">
            <el-form-item label="下限描述" prop="alarm_lower_info">
              <el-input v-model="form.alarm_lower_info" type="textarea" placeholder="请输入告警下限描述(0-255)" style="width: 200px;"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" id="alarmsetid">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  listAlarm,
  getAlarm,
  delAlarm,
  addAlarm,
  updateAlarm,
  changeAlarmStatus,
  listAlarms
} from "@/api/nssc/alarm/alarmSet/alarm";
import {listParamType} from "@/api/nssc/paramtype/paramtype";
import $ from "jquery";
import "@/assets/styles/robt-bg.css";
export default {
  name: "AlarmSet",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      param_names:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //初始化列表数据
      listAlarm: [],
      // 总条数
      total: 0,
      //添加或修改对话框-----参数类型下来数据
      alarmParamList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 8,
        site_id: undefined,
        process_body_id: undefined,
        robot_id: undefined,
        site_name: undefined,
        process_name: undefined,
        robot_name: undefined,
        status: undefined,
        robot_num: undefined
      },
      queryParams1: {
        robot_id: undefined,
        param_id: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        site_id: [
          { required: true, message: "站点名称不能为空"}
        ],
        process_body_id: [
          { required: true, message: "工艺名称不能为空"}
        ],
        robot_id: [
          { required: true, message: "机器人名称不能为空"}
        ],
        param_id: [
          { required: true, message: "参数类型不能为空"}
        ]
      }
    };
  },
  created() {
    this.getList();    /** 初始化告警设置列表 */
  },
  methods: {
    //自定义对话框关闭方法
    handleCancle () {
      $("#alarmsetid").css("display","");
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    //根据参数id、机器人id查询是否有关联报警参数
    getOptioncslx(param_id) {
      this.queryParams1.param_id = param_id;//设置站点id条件，查询工艺体
      listAlarms(this.queryParams1).then(response => {
        if(typeof(response)!="undefined" && response != null && response.rows.length>0) {
          $("#alarmsetid").css("display","none");
          this.$modal.confirm1('当前参数类型已创建！');
        }else {
          $("#alarmsetid").css("display","");
        }
      });
    },
    /**初始化告警设置列表*/
    getList() {
      this.loading = true;
      listAlarm(this.queryParams).then(response => {
        this.listAlarm = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /**添加或修改对话框--------初始化参数列表*/
    getParamList() {
      listParamType(this.queryParams).then(response => {
        this.alarmParamList = response.rows;
      });
    },
    //告警状态设置
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.alarmId + '"规则吗？').then(function() {
        return changeAlarmStatus(row.alarmId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    },// 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },// 表单重置
    reset() {
      this.form = {
        robot_id: undefined,
        param_id: undefined,
        alarmUpper: undefined,
        alarmLower: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.param_en=null;
      this.queryParams.param_name=null;
      this.queryParams.robot_id=null;
      this.queryParams.robot_name=null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.alarmId)
      this.param_names = selection.map(item => item.param_name)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增告警";
      this.getParamList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const alarmId = row.alarmId || this.ids
      getAlarm(alarmId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改告警";
        this.getParamList();//添加或修改对话框---------初始化下拉选择参数列表
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.alarmId != undefined) {
            updateAlarm(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAlarm(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.alarmId || this.ids;
      const param_names = row.param_name || this.param_names
      this.$modal.confirm('是否确认删除参数名称为"' + param_names + '"的告警设置数据项？').then(function() {
        return delAlarm(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
